
' ****************************************************************
' ***	DF SIMPLE UPDATE SCRIPT SAMPLE	***
' ***	***
' *** Author:	Faronics Corporation	***
' *** Date:	12/29/2005	***
' ***	***
' *** Associated Files:	***
' *** <workstationName>.mar - Used to indicate patch is running	***
' *** COMPLETE-<workstationName>.fin - Indicates patch complete	***
' *** DFC.exe - Deep Freeze Command Line Control	***
' ****************************************************************

' NOTES:
' The following script will turn off Deep Freeze, run updates and turn on Deep Freeze.

' ********** GLOBAL ASSEMBLIES **********
Set objNet = CreateObject("WScript.NetWork")

' ********** GLOBAL VARIABLES **********
' Modify the UNC path to match that of your server envrionment. 
strUNCPath = "\\FarDemo.local\NETLOGON\"
strMarkerFile = objNet.ComputerName & ".mar"
strMarkerCompleteFile = "COMPLETED-" & objNet.ComputerName & ".fin"

' ********** MAIN **********
' Calls all of the other routines...
If UpdateRunning = True Then
   RunPatch 
   RemoveMarker 
   BootFrozen
Else
   If UpdateComplete = False Then
      If UserPatchPrompt = True Then
         InsertMarker
         If Frozen = True Then
            BootThawed
         Else 
            RunPatch 
            RemoveMarker 
            BootFrozen
         End If
      Else
       ' Exit Script
      End If
   Else
    ' Exit Script
   End If
End If

' ********** UPDATE RUNNING? **********
' Check for marker file. If exists, the update is running. Return True. 
Function UpdateRunning
   Set objFS = CreateObject("Scripting.FileSystemObject") 
   Set objFolder = objFS.GetFolder(strUNCPath)
   Set objRE = new RegExp 
   objRE.Pattern = strMarkerFile 
   objRE.IgnoreCase = True

   For Each objFile In objFolder.Files
      If objRE.Test(objFile.Name) Then
         UpdateRunning = True
         Exit Function
      End If
   Next

   UpdateRunning = False
End Function

' ********** UPDATE COMPLETE? **********
' Checks for completed marker file. If it exists, the update has already run. 
Function UpdateComplete
   Set objFS = CreateObject("Scripting.FileSystemObject")
   Set objFolder = objFS.GetFolder(strUNCPath)
   Set objRE = new RegExp
   objRE.Pattern = strMarkerCompleteFile 
   objRE.IgnoreCase = True

   For Each objFile In objFolder.Files
      If objRE.Test(objFile.Name) Then
         UpdateComplete = True
         Exit Function
      End If
   Next

   UpdateComplete = False
End Function

' ********** USER PATCH PROMPT **********
' Prompt the user whether they would like to run the updates at this time. 
Function UserPatchPrompt 
   intAnswer=Msgbox("Anupdatehasbeendetected.Wouldyouliketoruntheupdatenow?" & vbLF &_
   "The update process will require several reboots!", vbYesNo, "Update Detected") 
   If intAnswer = vbYes Then
      UserPatchPrompt = True
      InsertMarker
   Else
      UserPatchPrompt = False
   End If
End Function
 
' ********** RUN PATCH **********
' The code to run the patches would occur here. 
Sub RunPatch
 ' Enter code to execute the patch(es)
 ' The next two lines would run a program by the name of update.exe
   Set objShell = CreateObject("Wscript.Shell")
   objShell.Run("update.exe") 
   MsgBox "Patch has been applied" 
   InsertCompleteMarker
End Sub

' ********** DEEP FREEZE FROZEN? **********
' Checks to see if Deep Freeze is Frozen and returns True or False. 
Function Frozen
   Set objShell = CreateObject("Wscript.Shell")
   intStatus = objShell.Run("DFC password /ISFROZEN", 1, True) 
   If intStatus = 0 Then 
     'DF is Thawed
      Frozen = False
   Else
      If intStatus = 1 Then 
        'DF is Frozen
         Frozen = True
      Else
        'A number of other reasons. 
      End If
   End If
End Function

' ********** BOOT FROZEN ********** 
Sub BootFrozen
   Set objShell = CreateObject("Wscript.Shell")
   objShell.Run("DFC password /BOOTFROZEN") 
End Sub

' ********** BOOT THAWED ********** 
Sub BootThawed
   Set objShell = CreateObject("Wscript.Shell")
   objShell.Run("DFC password /BOOTTHAWED") 
End Sub

' ********** INSERT MARKER **********
' Insert the marker file to indicate the patch is in progress. 
Sub InsertMarker
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objFile = objFSO.CreateTextFile(strUNCPath & strMARKERFILE) 
End Sub

'********** REMOVE MARKER **********
' Remove the marker file to indicate the patch is complete
Sub RemoveMarker
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   objFSO.DeleteFile(strUNCPath & strMarkerFile) 
End Sub

' ********** INSERT UPDATE COMPLETE MARKER **********
' This inserts an update completed file to prevent update looping
Sub InsertCompleteMarker
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objFile = objFSO.CreateTextFile(strUNCPath & strMarkerCompleteFile) 
End Sub

' ********** CLEANUP ********** 
Set objNet = Nothing
Set objFile = Nothing
Set objRE = Nothing
Set objFolder = Nothing
Set objTS = Nothing
Set objFS = Nothing
Set objTextFile = Nothing
Set objFSO = Nothing
